import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:persistent_bottom_nav_bar/persistent_bottom_nav_bar.dart';
// import 'package:persistent_bottom_nav_bar/persistent_tab_view.dart';
import '../../services/ScreenAdapter.dart';
import 'Home.dart';
import 'Category.dart';
import 'Cart.dart';
import 'User.dart';

class Tabs extends StatefulWidget {
  const Tabs({super.key});

  @override
  State<Tabs> createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
  //页面列表
  final List<Widget> _pageList = [
    // 首页
    const HomePage(),
    // 分类
    const CategoryPage(),
    // 购物车
    const CartPage(),
    // 个人中心
    const UserPage(),
  ];

  // 标题列表
  final List<String> _titleList = [
    'home'.tr,
    'categories'.tr,
    'shopping_cart'.tr,
    'mine'.tr,
  ];

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context); // 初始化屏幕适配

    return PersistentTabView(
      context,
      screens: _buildScreen(),
      items: _buildNavbarsItems(),
    );
  }

  // 页面
  List<Widget> _buildScreen() {
    return [
      _pageList[0],
      _pageList[1],
      _pageList[2],
      _pageList[3],
    ];
  }

  // 底部导航栏
  List<PersistentBottomNavBarItem> _buildNavbarsItems() {
    return [
      _navBarItem(iconData: FontAwesomeIcons.house, title: _titleList[0]),
      _navBarItem(iconData: FontAwesomeIcons.list, title: _titleList[1]),
      _navBarItem(
          iconData: FontAwesomeIcons.cartShopping, title: _titleList[2]),
      _navBarItem(iconData: FontAwesomeIcons.userLarge, title: _titleList[3]),
    ];
  }

  PersistentBottomNavBarItem _navBarItem({
    required IconData iconData,
    required String title,
  }) {
    return PersistentBottomNavBarItem(
      icon: Icon(iconData, size: 22),
      title: title,
      activeColorPrimary: const Color.fromRGBO(246, 246, 246, 1),
      inactiveColorPrimary: const Color.fromRGBO(187, 193, 202, 1),
      activeColorSecondary: const Color.fromRGBO(243, 82, 48, 1),
      textStyle: const TextStyle(fontWeight: FontWeight.bold),
    );
  }
}
